/**
 * TutorialScene.m
 * 
 * Revision History: 
 *
 * Date     Author       Version   Changes
 * ======== ============ ========= ================================================================
 * 04/01/10 Robin Mahony 2.0       Initial version.
 * 04/11/10 Robin Mahony 2.1       Updated to include new tutorial images
 * 04/11/10 Steve Hu     2.2	   Added Page Label, removed Next button in the last page and
 *								   prev butotn in the first page.
 * ======== ============ ========= ================================================================
 *
 */

#import "TutorialScene.h"
#import <SystemConfiguration/SystemConfiguration.h>
#include <netdb.h>
#define lastPage 22

@implementation TutorialScene
/* This method initializes a Sprite object, which loads the background image. Then it sets the 
 Sprite's on-screen location to the middle of the screen. */
- (id) init 
{
    self = [super init];
    
	if (self != nil) 
	{
		bg = [Sprite spriteWithFile:@"p1.png"];
		[bg setPosition:ccp (160, 240) ];
		[self addChild:bg z:0];
		pageIndex = 1;
		
		/* Add a label to show page number. */
		pageLabel = [Label labelWithString:@"Page 01" fontName:@"Marker Felt" fontSize:16];
		[pageLabel setRGB:0 :0 :0];
		pageLabel.position = ccp (290, 11); 
		[self addChild: pageLabel];
		
		/* Create Next, Back, and Return to Menu buttons with corresponding button images.  */
		[MenuItemFont setFontSize: 24];
		nextButton = [MenuItemFont itemFromString:@"Next" target:self selector:@selector(nextCallback:)];
		prevButton = [MenuItemFont itemFromString:@"Prev" target:self selector:@selector(prevCallback:)];
		menuButton = [MenuItemFont itemFromString:@"Menu" target:self selector:@selector(menuCallback:)];
		menu = [Menu menuWithItems: menuButton, prevButton, nextButton, nil];
		prevButton.visible = NO;
		
		/* Set the padding between buttons.  */
		[menu alignItemsHorizontallyWithPadding:20];
		menu.position = ccp(160, 11);
		[self addChild:menu];
    }
    return self;
}


/* This method removes all objects added to the scene, indicating you want Cocos2D to “clean up” by 
 releasing the objects as well, and then you must call the dealloc method of the superclass  */
- (void) dealloc 
{
	[self removeAllChildrenWithCleanup:YES];
	[super dealloc];
}


/* prev button event handler for going to the previous page.  */
- (void) prevCallback:(id)sender 
{
	NSLog(@"prev pressed");
	nextButton.visible = YES;
	
	if(pageIndex > 1)
	{
		/*Decrement the page number, and load the new tutorial image.  */
		--pageIndex;
		NSLog(@"index greater than 1");
		NSString *filename = [[NSString alloc] initWithFormat:@"p%d.png", pageIndex];
		bg.texture=[[TextureMgr sharedTextureMgr] addImage: filename];
		
		/* Update the page number label.  */
		NSString *tempStr = [[NSString alloc] initWithFormat:@"Page %02d", pageIndex];
		[pageLabel setString:tempStr];
		[pageLabel draw];
		/* Make the prevButton invisible if it is the first page of tutorial.  */
		if (pageIndex == 1)
		{
			prevButton.visible = NO;
		}
	}
}

/*  next button event handler for going to the next page .  */
- (void) nextCallback:(id)sender 
{
	NSLog(@"next pressed");
	prevButton.visible = YES;

	if(pageIndex < lastPage)
	{
		/*Increment the page number, and load the new tutorial image.  */
		++pageIndex;
		NSLog(@"index less than lastPage");
		NSString *filename = [[NSString alloc] initWithFormat:@"p%d.png", pageIndex];
		bg.texture=[[TextureMgr sharedTextureMgr] addImage: filename];
		/* Make the nextbutton invisible if it is last page of tutorial.  */

		if (pageIndex == lastPage)
		{
			nextButton.visible = NO;

			/* Update the page number label to show end of page.  */
			NSString *tempStr = [[NSString alloc] initWithFormat:@"THE END", pageIndex];
			[pageLabel setString:tempStr];
			[pageLabel draw];
		}
		else
		{
			/* Update the page number label.  */
			NSString *tempStr = [[NSString alloc] initWithFormat:@"Page %02d", pageIndex];
			[pageLabel setString:tempStr];
			[pageLabel draw];
		}
		

	}
}

/* menu button event handler for returning to the main menu.  */
- (void) menuCallback:(id)sender 
{
	[[Director sharedDirector] replaceScene: [MenuScene node]];
}

@end
